package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.constant.AutoLog;
import com.yidu.ruifeng_logistics.entity.LogisticsCar;
import com.yidu.ruifeng_logistics.service.LogisticsCarService;
import com.yidu.ruifeng_logistics.service.LogisticsDriverService;
import com.yidu.ruifeng_logistics.service.LogisticsStaffService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.vo.DriverVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsCar)车辆表控制层
 *
 * @author makejava 夏杰明
 * @since 2021-03-09 14:24:36
 */
@RestController
@RequestMapping("logisticsCar")
public class LogisticsCarController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsCarService logisticsCarService;
    @Resource
    private LogisticsDriverService logisticsDriverService;

    @Resource
    private LogisticsStaffService logisticsStaffService;

    /**
     * 分页列表查询
     * @param logisticsCar
     * @param page
     * @param limit
     * @return
     */
    @AutoLog(value = "车辆表-分页列表查询")
    @ApiOperation(value = "车辆表-分页列表查询", notes = "车辆表-分页列表查询")
    @GetMapping(value = "/queryAll")
    public Map<String, Object> queryAll(LogisticsCar logisticsCar, int page, int limit,String keyWord){
        //将参数传入到分页助手
        PageHelper.startPage(page,limit);
        logisticsCar.setKeyWord(keyWord);
        List<LogisticsCar> list = logisticsCarService.queryAll(logisticsCar);
        Map<String, Object> map = new HashMap<>();
        PageInfo<LogisticsCar> pageInfo = new PageInfo<>(list);
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",pageInfo.getList());//可以直接写list
        map.put("count",pageInfo.getTotal()); //总行数
        return map;
    }

    @ApiOperation(value = "数据回显", notes = "返回一条数据")
    @PostMapping("/show")
    public BaseMessage show(Integer id){
        LogisticsCar logisticsCar = logisticsCarService.queryById(id);
        logisticsCar.setStaffName
                (logisticsStaffService.queryById(logisticsDriverService.queryById
                        (logisticsCar.getDriverId()).getStaffId()).getStaffName());
        return   BaseMessage.success(logisticsCar);
    }


    @ApiOperation(value = "增加或修改", notes = "返回状态信息")
    @PostMapping("/addAndUpdate")
    public BaseMessage addAndUpdate(LogisticsCar logisticsCar){
        if (StringUtils.isEmpty(logisticsCar.getCarId())){
            logisticsCar.setDriverId(0);
            logisticsCarService.insert(logisticsCar);
        }else {
            logisticsCarService.update(logisticsCar);
        }
        return BaseMessage.success();
    }

    /**
     *
     * @return
     */
    @ApiOperation(value = "返回司机选择框需要的数据", notes = "返回司机的名字")
    @PostMapping(value = "/selectDriver")
    public BaseMessage selectDriver(){
        List<DriverVo> list = logisticsDriverService.queryAll(new DriverVo());
        return BaseMessage.success(list);
    }


    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @PostMapping(value = "/deleteById")
    public BaseMessage deleteById(int id){
        return logisticsCarService.deleteById(id)?BaseMessage.success():BaseMessage.fail();
    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
    @PostMapping(value = "/deleteAllId")
    public BaseMessage deleteAllId(String ids){
        return logisticsCarService.deleteAllId(ids)>0?BaseMessage.success():BaseMessage.fail();
    }

}
